# -*- coding: utf-8 -*-
import random
import string
from django.core.mail import send_mail

from users.models import EmailVerifyRecord
from Education.settings import EMAIL_FROM


def send_type_email(email, send_type='register'):
    email_record = EmailVerifyRecord()
    if send_type == 'update_email':
        code = random_str(4)
    else:
        code = random_str(16)
    email_record.code = code
    email_record.email = email
    email_record.send_type = send_type
    email_record.save()

    if send_type == 'register':
        email_title = '激活链接'
        email_body = '请点击下面的链接激活你的账号: http://127.0.0.1:8000/active/%s' % code
        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email, ])
        if send_status:
            pass
    elif send_type == 'forget':
        email_title = '找回密码'
        email_body = '请点击下面的链接重置你的密码: http://127.0.0.1:8000/reset/%s' % code
        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email, ])
        if send_status:
            pass
    elif send_type == 'update_email':
        email_title = '邮箱修改验证码'
        email_body = '邮箱验证码为： {0}'.format(code)
        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email, ])
        if send_status:
            pass


def random_str(randomlength=8):
    code = ''
    chars = string.ascii_letters + '0123456789'
    for i in range(randomlength):
        code = code + random.choice(chars)
    return code

